package com.exercise;

import java.io.*;
import java.util.ArrayList;
/*
*
*       InputStream OutputStream
        FileInputStream   FileOutputStream  (字节文件输入输出流，-》图片视频音频压缩包exe)
        BufferedInputStream   BufferedOutputStream   // 对上面的节点流进行包装，加快效率


        Reader Writer   文本文档
        InputStreamReader     OutputStreamWriter   （x,charset）
        FileReader            FileWriter             （x）  用jvm默认编码
* */
public class Demo01 {
    public static String reverseStr(String str){
        String newStr = "";
        for(int i = str.length(); i > 0; i--){
            newStr += str.charAt(i-1);
        }
        return newStr;
    }
    public static void main(String[] args) {
        BufferedReader bufReader = null;
        BufferedWriter bufWriter = null;
        ArrayList<String> lines = new ArrayList<String>();
        try {
            bufReader = new BufferedReader(new FileReader("d:/tangshi.txt"));
            bufWriter = new BufferedWriter(new FileWriter("d:/tangshi-new.txt"));

            String result;
            while((result = bufReader.readLine()) != null){
                lines.add(reverseStr(result));
            }
            for(int i = lines.size(); i > 0; i--){
                bufWriter.write(lines.get(i-1));
                bufWriter.newLine();
                bufWriter.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bufReader != null){
                try {
                    bufReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufWriter != null){
                try {
                    bufWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
